<?php
defined('BASEPATH') OR exit('No direct script access allowed');


class LawerHelper extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->model('Users_Model');
		$this->load->model('Comment_Model');
		$this->load->model('Question_Model');
		$this->load->helper('url_helper');
		$this->load->library('session');
	}
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 *        http://example.com/index.php/welcome
	 *    - or -
	 *        http://example.com/index.php/welcome/index
	 *    - or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function get_user_list($role,$start_row,$number){
		$data['start_row']=$start_row;
		$data['number']=$number;
		$data['role']=$role;
		$users=$this->Users_Model->get_users($data);
		return $users;
	}
	public function index()
	{
		$name = $this->session->userdata('username');
		$data=array();
		$target='';
		if(isset($name)){
			$data['title']='司法援助平台';
			$target='lawhelp';
			$sql['start_row']=0;
			$sql['number']=15;
			$sql['sort']='gmt_modify desc';
			$sql['role']=1;
			$users=$this->Users_Model->get_users($sql);
			$data['users']=$users;
		}else{
			$data['title']='登录';
			$target='lawer_help_login';
		}
		$this->load->view('head_sec',$data);
		$this->load->view($target,$data);
		$this->load->view('foot_sec',$data);
	}

	public function new_question($id){
		$data['title']="新建咨询";
		$data['support']=$id;
		$this->load->view('head_sec',$data);
		$this->load->view('lawer_help_new_question',$data);
		$this->load->view('foot_sec',$data);
	}

	public function get_question_list(){
		$data['asker']=$this->session->userdata('userid');
		$data['start_row']=0;
		$data['number']=15;
		$data['sort']='gmt_modify desc';
		$questions=$this->Question_Model->get_questions($data);
		$result=array();
		$result['isSuccess']=true;
		$result['errorMessage']='';
		$result['data']=$questions;
		echo json_encode($result);
		return;
	}

	public function question_detail($id){
		$question=$this->Question_Model->get_question_by_id($id);
		$question_id=$question['id'];
		$asker=$question['asker'];
		$user=$this->Users_Model->get_user_by_id($asker);
		$asker_name=$user['real_name'];
		$data['q_id']=$question_id;
		$comments=$this->Comment_Model->get_comments($data);
		$comments_details=array();
		foreach ($comments as $item):
			$comment_detail=$item;
			$user=$this->Users_Model->get_user_by_id($item['commentor']);
			$real_name=$user['real_name'];
			$comment_detail['real_name']=$real_name;
			array_push($comments_details,$comment_detail);
		endforeach;
		$data['title']=$question['q_title'];
		$data['question']=$question;
		$data['comments']=$comments_details;
		$data['asker_name']=$asker_name;
		$this->load->view('head_sec',$data);
		$this->load->view('question_detail',$data);
		$this->load->view('foot_sec',$data);
	}

	public  function save_question(){
		if(!isset($_POST['q_title'])||strlen($_POST['q_title'])<4 ||strlen($_POST['q_title'])>100 ){
			$result=array();
			$result['isSuccess']=false;
			$result['errorMessage']='咨询标题字数不符合要求！';
			$result['data']='';
			echo json_encode($result);
			return;
		}
		$q_title=$_POST['q_title'];
		if(!isset($_POST['q_type'])||($_POST['q_type']=='')){
			$result=array();
			$result['isSuccess']=false;
			$result['errorMessage']='您还没有选择问题类别！';
			$result['data']='';
			echo json_encode($result);
			return;
		}
		$q_type=$_POST['q_type'];
		if(!isset($_POST['q_content'])||($_POST['q_content']=='')){
			$result=array();
			$result['isSuccess']=false;
			$result['errorMessage']='问题内容不能为空！';
			$result['data']='';
			echo json_encode($result);
			return;
		}
		if(!isset($_POST['support'])||($_POST['support']=='')){
			$result=array();
			$result['isSuccess']=false;
			$result['errorMessage']='没有获取到援助者！';
			$result['data']='';
			echo json_encode($result);
			return;
		}
		$support=$_POST['support'];
		$q_content=$_POST['q_content'];
		$data['q_type']=$q_type;
		$data['q_title']=$q_title;
		$data['q_content']=$q_content;
		$data['supportor']=$support;
		$data['asker']=$this->session->userdata('userid');
		$data['is_deleted']='N';
		$data['gmt_create']=date("Y-m-d H:i:s" ,time());
		$data['gmt_modify']=date("Y-m-d H:i:s" ,time());

		$question_id=$this->Question_Model->add_question($data);
		if(!($question_id)){
			$result=array();
			$result['isSuccess']=false;
			$result['errorMessage']='提交问题失败';
			$result['data']='';
			echo json_encode($result);
			return;
		}else{
			$result=array();
			$result['isSuccess']=true;
			$result['errorMessage']='';
			$data['question_id']=$question_id;
			$result['data']=$data;
			echo json_encode($result);
			return;
		}
	}

	public function login()
	{
		$data['title']="登录";
		$this->load->view('head_sec',$data);
		$this->load->view('lawer_help_login',$data);
		$this->load->view('foot_sec',$data);
	}

	public function makeLogin(){
		header('content-type:application:json;charset=utf8');
		header('Access-Control-Allow-Origin:*');
		header('Access-Control-Allow-Methods:POST');
		header('Access-Control-Allow-Headers:x-requested-with,content-type');
		if(!isset($_POST['username'])||strlen($_POST['username'])<4){
			$result=array();
			$result['isSuccess']=false;
			$result['errorMessage']='用户名不能少于4位！';
			$result['data']='';
			echo json_encode($result);
			return;
		}
		$username=$_POST['username'];
		if(!isset($_POST['password'])||strlen($_POST['password'])<6){
			$result=array();
			$result['isSuccess']=false;
			$result['errorMessage']='密码不能少于6位！';
			$result['data']='';
			echo json_encode($result);
			return;
		}
		$password=$_POST['password'];
		$data['username']=$username;
		$data['password']=$password;
		$users=$this->Users_Model->get_users($data);
		if(count($users)==0){
			$result=array();
			$result['isSuccess']=false;
			$result['errorMessage']='用户名密码不正确';
			$result['data']='';
			echo json_encode($result);
			return;
		}else{
			$newdata = array(
				'username'  =>$users[0]['username'],
				'role'     => $users[0]['role'],
				'realname'=>$users[0]['real_name'],
				'userid'=>$users[0]['id'],
			);
			$this->session->set_userdata($newdata);
			$result=array();
			$result['isSuccess']=true;
			$result['errorMessage']='';
			$result['data']='';
			echo json_encode($result);
			return;
		}
	}
}


